package com.atguigu.msmservice.service.impl;

/*
 * @ClassName MsmServiceImpl
 * @author  忘忧君
 * @Description
 * @Version 1.0
 */

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.atguigu.msmservice.service.MsmService;
import com.atguigu.msmservice.utils.MailRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;
import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {

    private static final Logger logger = LoggerFactory.getLogger(MsmServiceImpl.class);

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String sendMailer;

    @Override
    public boolean send(Map<String, Object> param, String phone) {
        if(StringUtils.isEmpty(phone)) return false;

        DefaultProfile profile =
                DefaultProfile.getProfile("default", "LTAIq6nIPY09VROj", "FQ7UcixT9wEqMv9F35nORPqKr8XkTF");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "sgin name");
        request.putQueryParameter("TemplateCode", "template code");
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return false;
    }


    @Override
    public boolean sendSimpleMail(Map<String, Object> param, String email) {

        if(StringUtils.isEmpty(email)) return false;

        System.out.println(email);
        MailRequest mailRequest = new MailRequest();
        mailRequest.setSendTo(email);
        mailRequest.setSubject("辅助注册验证");
        mailRequest.setText("尊敬的用户，您的注册会员动态密码为："+ param.get("code") + "，请勿泄漏于他人！");

        SimpleMailMessage message = new SimpleMailMessage();

        System.out.println(mailRequest.getSendTo());
        checkMail(mailRequest);

        message.setFrom(sendMailer); //邮件发件人
        message.setTo(mailRequest.getSendTo().split(",")); // 邮件收件人 1或多个
        message.setSubject(mailRequest.getSubject()); //邮件主题
        message.setText(mailRequest.getText()); //邮件内容
        message.setSentDate(new Date()); //邮件发送时间

        javaMailSender.send(message);
        logger.info("发送邮件成功:{}->{}",sendMailer,mailRequest.getSendTo());

        return true;
    }

    public void checkMail(MailRequest mailRequest) {
        Assert.notNull(mailRequest,"邮件请求不能为空");
        Assert.notNull(mailRequest.getSendTo(), "邮件收件人不能为空");
        Assert.notNull(mailRequest.getSubject(), "邮件主题不能为空");
        Assert.notNull(mailRequest.getText(), "邮件收件人不能为空");
    }
}
